package cn.webrx;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import javax.swing.*;
import java.io.IOException;

public class App {
    public static void main(String[] args) throws IOException {
        //book 书，书籍 爬虫 jsoup组件
        //1 idea建立项目，建立lib，复制下载jsoup.jar组件


        //2 声明变量
        String word = JOptionPane.showInputDialog("请输入要翻译的词汇：");

        while(word!=null && word.length()>0) {

            //3 http://cn.bing.com/dict/search?q=book
            String url = "http://cn.bing.com/dict/search?q=" + word;
            //String url = "https://www.so.com/s?q=" + word;

            //String url = "http://wx/dict/" + word;


            //4 爬取翻译结果
            Elements es = Jsoup.connect(url).get().select("span[class=def b_regtxt]");
            //Elements es = Jsoup.connect(url).get().select("span[class=mh-translation-txt]");

            //使用StringBuilder 循环读取，词汇的所有翻译
            StringBuilder sbu = new StringBuilder();
            for (Element e : es) {
                sbu.append(e.text() + "\r\n");
            }
            //
            //String result = Jsoup.connect(url).get().select("body").get(0).text();
            //JOptionPane.showMessageDialog(null,result);


            //5 输出翻译结果
            //System.out.println(result);
            JOptionPane.showMessageDialog(null, sbu.toString());
            word = JOptionPane.showInputDialog("请输入要翻译的词汇：");
        }

        JOptionPane.showMessageDialog(null, "程序退出");
        System.exit(0);
    }
}
